package com.poi;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import javax.swing.filechooser.FileSystemView;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;

public class ExcelWrite {
    public static void main(String[] args) {
    }

    /**        获取桌面位置 方便把写入的excel文件输出到桌面
     *         FileSystemView fsv=FileSystemView.getFileSystemView();
     *         String path=fsv.getHomeDirectory().getAbsolutePath();
     *         System.err.println(path);
     */
    private static String Path="E:\\Project\\ljj-first-poi\\";
    @Test
    public void ExcelWriteTest() throws IOException {
        //1.创建工作博
        Workbook workbook=new HSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet=workbook.createSheet("ljj表");
        //3.创建一个行
        Row row=sheet.createRow(0);
        //4.创建一个单元格
        Cell cell=row.createCell(1);
        cell.setCellValue("测试");

        Row row1=sheet.createRow(1);
        Cell cell1=row1.createCell(1);
        cell1.setCellValue("统计时间");

        Cell cell21=row1.createCell(2);
        String time =new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell21.setCellValue(time);

        //生成一张表（通过io流）
        FileOutputStream fileOutputStream=new FileOutputStream(Path+"test.xls");
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();
        System.out.println("excel");
    }

    @Test
    public void ExcelWriteTestxlsx() throws IOException {
        //1.创建工作博
        Workbook workbook=new XSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet=workbook.createSheet("ljj表");
        //3.创建一个行
        Row row=sheet.createRow(0);
        //4.创建一个单元格
        Cell cell=row.createCell(1);
        cell.setCellValue("测试");

        Row row1=sheet.createRow(1);
        Cell cell1=row1.createCell(1);
        cell1.setCellValue("统计时间");

        Cell cell21=row1.createCell(2);
        String time =new DateTime().toString("yyyy-MM-dd HH:mm:ss");
        cell21.setCellValue(time);

        //生成一张表（通过io流）
        FileOutputStream fileOutputStream=new FileOutputStream(Path+"ljjtest.xlsx");
        workbook.write(fileOutputStream);
        //关闭流
        fileOutputStream.close();
        System.out.println("excel");
    }
    @Test
    public void ExcelWriteTestBigData() throws IOException {

        //时间
        long begin =System.currentTimeMillis();
        //1.创建工作博
        Workbook workbook=new HSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet=workbook.createSheet();
        //写入数据
        for (int rowNum=0;rowNum<65536;rowNum++){
        Row row=sheet.createRow(rowNum);
        for (int cellNum=0;cellNum<10;cellNum++){
            Cell cell=row.createCell(cellNum);
            cell.setCellValue(cellNum);
        }
        }
        System.out.println("完成");
        FileOutputStream fileOutputStream=new FileOutputStream(Path+"test.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        long end=System.currentTimeMillis();
        System.err.println("运行耗费（秒）："+(double) (end-begin)/1000);
    }

    @Test
    //加速
    public void ExcelWriteTestBigDataS() throws IOException {

        //时间
        long begin =System.currentTimeMillis();
        //1.创建工作博
        Workbook workbook=new SXSSFWorkbook();
        //2.创建一个工作表
        Sheet sheet=workbook.createSheet();
        //写入数据
        for (int rowNum=0;rowNum<65536;rowNum++){
            Row row=sheet.createRow(rowNum);
            for (int cellNum=0;cellNum<10;cellNum++){
                Cell cell=row.createCell(cellNum);
                cell.setCellValue(cellNum);
            }
        }
        System.out.println("完成");
        FileOutputStream fileOutputStream=new FileOutputStream(Path+"test.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        //清除临时文件
        ((SXSSFWorkbook)workbook).dispose();
        long end=System.currentTimeMillis();
        System.err.println("运行耗费（秒）："+(double) (end-begin)/1000);
    }

}
